package cn.stylefeng.guns.modular.category.controller;

import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.constant.dictmap.factory.CategoryDict;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.modular.system.warpper.CategoryWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Category;
import cn.stylefeng.guns.modular.category.service.ICategoryService;

import java.util.List;
import java.util.Map;

/**
 * 商品类别控制器
 *
 * @author fengshuonan
 * @Date 2020-04-16 16:17:22
 */
@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {

    private String PREFIX = "/category/category/";

    @Autowired
    private ICategoryService categoryService;

    /**
     * 跳转到商品类别首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "category.html";
    }

    /**
     * 跳转到添加商品类别
     */
    @RequestMapping("/category_add")
    public String categoryAdd() {
        return PREFIX + "category_add.html";
    }

    /**
     * 跳转到修改商品类别
     */
    @RequestMapping("/category_update/{categoryId}")
    public String categoryUpdate(@PathVariable Integer categoryId, Model model) {
        Category category = categoryService.selectById(categoryId);
        model.addAttribute(category);
        model.addAttribute("pName", ConstantFactory.me().getCategoryName(category.getPid()));
        LogObjectHolder.me().set(category);
        return PREFIX + "category_edit.html";
    }

    /**
     * 获取类别的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.categoryService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 获取商品类别列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.categoryService.list(condition);
        return super.warpObject(new CategoryWrapper(list));
    }

    /**
     * 新增商品类别
     */
    @BussinessLog(value = "添加商品类别", key = "simplename", dict = CategoryDict.class)
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Category category) {
        if (ToolUtil.isOneEmpty(category, category.getSimplename())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        categorySetPids(category);
        return this.categoryService.insert(category);
    }

    /**
     * 删除商品类别
     */
    @BussinessLog(value = "删除商品类别", key = "categoryId", dict = CategoryDict.class)
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer categoryId) {
        categoryService.deleteById(categoryId);
        return SUCCESS_TIP;
    }

    /**
     * 修改商品类别
     */
    @BussinessLog(value = "修改商品类别", key = "simplename", dict = CategoryDict.class)
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Category category) {
        if (ToolUtil.isEmpty(category) || category.getId() == null) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        categorySetPids(category);
        categoryService.updateById(category);
        return SUCCESS_TIP;
    }

    /**
     * 商品类别详情
     */
    @RequestMapping(value = "/detail/{categoryId}")
    @ResponseBody
    public Object detail(@PathVariable("categoryId") Integer categoryId) {
        return categoryService.selectById(categoryId);
    }

    private void categorySetPids(Category category) {
        if (ToolUtil.isEmpty(category.getPid()) || category.getPid().equals(0)) {
            category.setPid(0);
            category.setPids("[0],");
        } else {
            int pid = category.getPid();
            Category temp = categoryService.selectById(pid);
            String pids = temp.getPids();
            category.setPid(pid);
            category.setPids(pids + "[" + pid + "],");
        }
    }
}
